package com.lhf.person.service.impl;

import com.lhf.person.dao.PersonDao;
import com.lhf.person.entity.Person;
import com.lhf.person.service.PersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;


@Service
public class PersonServiceImpl implements PersonService {
    @Autowired
    private PersonDao dao;
    @Override
    public Page<Person> foundPerson(String name, int sex, int pageindex) {
        Page<Person> byNameAndSex=null;
        PageRequest pageRequest = this.getPageRequest(pageindex, 2);
        System.out.println("service层已经调用"+name+","+sex+","+pageindex);
        if (name!=""&&sex!=-1){
            //名字和性别都有
            byNameAndSex = dao.findByNameAndSex("%"+name+"%", sex,pageRequest);
        }else if (name!=""&&sex==-1){
            //如果有名字
            byNameAndSex = dao.findByName("%"+name+"%",pageRequest);
        }else if (sex!=-1&&name==""){
            //如果有性别
            byNameAndSex = dao.findBySex(sex,pageRequest);
        }else{
            //如果名字和性别都没有
            System.out.println("service层已经调用");
            byNameAndSex = dao.findAll(pageRequest);
        }
        return byNameAndSex;
    }

    @Override
    public String getintroduce(int id) {
        return dao.findintroduce(id);
    }
    //jpa分页对象,可以选择有参构造，但在2.0以后官方推荐使用静态方法of
    public PageRequest getPageRequest(int pageindex, int pagesize){
        return new PageRequest(pageindex-1, pagesize, Sort.Direction.ASC,"id");
    }
}
